#!perl
use strict;
use File::Basename;
use Cwd 'abs_path';

my $bindir = abs_path(dirname($0));
my %branches = ();

open F, "<branches.txt" or die;
while (my $br = <F>) {
    $br =~ s/\#.*//s;
    $br =~ s/^\s*//s;
    $br =~ s/\s*$//s;
    my @brs = split /[\s,]+/s, $br;
    for $br (@brs) {
        $branches{$br} = 1 unless ($br eq '');
    }
}
close F;
my @branches = keys %branches;

open F, '>branches.txt' or die;
print F join "\n", @branches;
print F "\n";
close F;

open F, ">compute.sh" or die;
print F <<"EOF";
time (cat branches.txt | perl -I$bindir $bindir/graphdb.pl >nodes0.txt 2>graphdb-spent-times.txt)
time (cat nodes0.txt   | perl -I$bindir $bindir/mkdot.pl --rewrite nodes.txt)

time (cat nodes.txt | perl -I$bindir $bindir/mkdot.pl                  >b.dot)
time (cat nodes.txt | perl -I$bindir $bindir/mkdot.pl --curves         >b-curved.dot)
time (cat nodes.txt | perl -I$bindir $bindir/mkdot.pl --slim           >bslim.dot)
time (cat nodes.txt | perl -I$bindir $bindir/mkdot.pl --slim  --curves >bslim-curved.dot)
time (cat nodes.txt | perl -I$bindir $bindir/mkdot.pl --slim5          >bslim5.dot)
time (cat nodes.txt | perl -I$bindir $bindir/mkdot.pl --slim5 --curves >bslim5-curved.dot)
EOF
close F;
system "bash -x compute.sh >compute.log 2>&1";

my $f;

$f = 'b';             dot2svg($f);
$f = 'b-curved';      dot2svg($f);
$f = 'bslim';         dot2svg($f);
$f = 'bslim-curved';  dot2svg($f);
$f = 'bslim5';        dot2svg($f);
$f = 'bslim5-curved'; dot2svg($f);

sub dot2svg
{
    my $f = shift;
    system "mkdir -p svgs";
    system "dot -Tsvg -osvgs/${f}0.svg $f.dot"; system "perl $bindir/postprocsvg.pl <svgs/${f}0.svg >svgs/$f.svg";
}

